package com.neuedu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Demo2 {
    public static void main(String[] args) {
        try {
            // 构建配置对象
            Configuration conf = new Configuration();
            // 配置hadoop集群属性，若提供配置文件时，会自动读取
            //conf.set(“fs.defaultFs”，“hdfs://master:9000");
            // 获取HDFS对象
            FileSystem hdfs = FileSystem.get(conf);
            // 定义目标文件
            Path dst = new Path("hdfs://master:9000/f.txt");
            // 打开文件
            FSDataInputStream inputStream = hdfs.open(dst);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            // 循环读取文件内容
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            // 关闭流
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
